﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using ueEngine.Biz.Core.AppService.Member;
using ueEngine.Web.WebLogic.Model;
using ueEngine.Web.WebLogic;
using ueEngine.Biz.Core.AppService.Recruitment;
using ueEngine.Biz.Inf.LogicEntity;
using ueEngine.Framework.Common;
using System.Collections.Specialized;

namespace ueEngine.Web.Controllers
{
    //[TraceBehavior]
    [ErrorHandle]
    [Compress]
    [Authorization(AuthorizeRole.Company)]
    public class CompanyCenterController : Controller
    {
        //
        // GET: /CompanyCenter/

        public ActionResult Home()
        {
            ViewData["UserCenterSiteMap"] = new List<UserCenterSiteMapVModel>() { 
                new UserCenterSiteMapVModel()
                {
                    DisplayName="企业中心",
                    Link="/CompanyCenter/Home"
                }
            };
            return View();
        }
        
        #region 招募管理
        public ActionResult Recruit(int page=0)
        {
            ViewData["UserCenterSiteMap"] = new List<UserCenterSiteMapVModel>() { 
                new UserCenterSiteMapVModel()
                {
                    DisplayName="企业中心",
                    Link="/CompanyCenter/Home"
                },
                new UserCenterSiteMapVModel()
                {
                    DisplayName="招募管理",
                    Link="/CompanyCenter/Recruit"
                }
            };

            //正在进行的招募
            var Recruiting = new RecruitService(HttpContext.User.Identity.Name).QueryRecruitment(page, 12, RecruitSheetType.NoDefine,false,false);
            ViewData["Recruiting"] = Recruiting;
            var NoVerify =new RecruitService(HttpContext.User.Identity.Name).QueryRecruitment(page, 12, RecruitSheetType.NoDefine, true,false);
            ViewData["NoVerify"] = NoVerify;
            return View();
        }
        public ActionResult EditRecruiting(int id)
        {
            ViewData["UserCenterSiteMap"] = new List<UserCenterSiteMapVModel>() { 
                new UserCenterSiteMapVModel()
                {
                    DisplayName="企业中心",
                    Link="/CompanyCenter/Home"
                },
                new UserCenterSiteMapVModel()
                {
                    DisplayName="招募管理",
                    Link="/CompanyCenter/Recruiting"
                },
                new UserCenterSiteMapVModel()
                {
                    DisplayName="编辑招募",
                    Link="/CompanyCenter/EditRecruiting"
                }
            };
            //代理企业信息
            var employerData = new RecruitService(HttpContext.User.Identity.Name).GetTempleteCompanies(0, 8);
            ViewData["EmployerStr"] = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(employerData);

            //历史招募单
            var recruitingTemplete = new RecruitService(HttpContext.User.Identity.Name).GetTempleteSheetes(0, 8);
            ViewData["RecruitingStr"] = recruitingTemplete.ToJson();//new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(recruitingTemplete);
            
            var item = new RecruitService(HttpContext.User.Identity.Name).GetRecruitment(id);
            item.DepositCompany.CompanyDetail = item.DepositCompany.CompanyDetail.Replace("\"", "");
            ViewData["Original"] = item.ToJson();//new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(item);
            return View();
        }
        public int maketop(int id)
        {
            var recruitment = new RecruitSheetModel()
                .Element(c => c.Id == id);// new RecruitService(HttpContext.User.Identity.Name).GetRecruitment(id);
            if (recruitment.Refresh.DayOfYear != DateTime.Now.DayOfYear)
            {
                recruitment.Refresh = DateTime.Now;
                recruitment.PostModify();
                return 1;
            }
            return 0;
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult EditRecruiting(int Original, RecruitingVModel model)
        {
            DepositCompanyModel depositCompany = new DepositCompanyModel()
            {
                Agent = HttpContext.User.Identity.Name,
                Id = model.CreateCompanyTemp == "on" ? model.DepositCompanyId : 0,
                CompanyBusiness = (Business)model.CompanyBusiness,
                CompanyDetail = model.CompanyDetail,
                CompanyDimensions = (CompanyDimensions)model.CompanyDimensions,
                CompanyName = model.CompanyName,
                CompanyType = (CompanyType)model.CompanyType,
                PostDate = DateTime.Now,
                Templete = model.CreateCompanyTemp == "on"
            };

            RecruitSheetModel recruitSheet = new RecruitSheetModel()
            {
                //Description = model.Description,
                Templete = model.CreateTempelte == "on",
                Caption = model.Caption,
                PublishDate = DateTime.Now,
                Recruiter = HttpContext.User.Identity.Name,
                RecruitType = model.RecruitType == "on" ? RecruitSheetType.Durable : RecruitSheetType.Normal,// RecruitSheetType.Normal,
                EnableEmail = model.EnableEmail == "on" ? true : false,
                RecruitSubject = RecruitSheetSubject.Default,
                WorkArea = (AreaCode)model.WorkArea,
                WorkAddress = model.WorkAddress,
                Contact = model.ContactMan,
                ContactPhone = model.ContactPhone,
                DepositCompany = depositCompany,
                ReceiveEmail = model.ReceiveEmail,
                QQ = model.QQ,
                Positions = CollectPosition(model.Position, Request.Form)
                    .Select(c => new PositionModel()
                    {
                        Degree = (Degree)c.Degree,
                        Experienced = c.Experienced == "on",
                        Gender = (Gender)c.Gender,
                        PositionDescript = c.PositionDescript,
                        PositionName = c.PositionName,
                        PositionType = (PositionType)c.PositionType,
                        IsIndex = false,
                        MaxSalary = c.MaxSalary,
                        MinSalary = c.MinSalary,
                        OperatorName = String.Empty,
                        Period = (SalaryPeriod)c.Period,
                        RecruitId = 0,
                        Trusteeship = false,
                        Vacancy = c.Vacancy,
                        ScanTime = 0,
                        Priority = (PositionPriority)(c.PositionPriority == "on" ? 1 : 0),
                        PositionLevel = PositionLevel.Normal
                    }).ToList(),
            };

            if (new RecruitService(HttpContext.User.Identity.Name)
                .UpdateRecruitment(Original, recruitSheet) != null)
            {
                return Redirect("/companycenter/editsuccess");
            }
            else
            {
                return Redirect("/companycenter/editfaild");
            }
        }
        /// <summary>
        /// 添加招募信息成功
        /// </summary>
        /// <returns></returns>
        public ActionResult EditSuccess()
        {
            ViewData["UserCenterSiteMap"] = new List<UserCenterSiteMapVModel>() { 
                new UserCenterSiteMapVModel()
                {
                    DisplayName="企业中心",
                    Link="/CompanyCenter/Home"
                },
                new UserCenterSiteMapVModel()
                {
                    DisplayName="发布招募成功",
                    Link="/CompanyCenter/PostSuccess"
                }
            };
            return View();
        }
        /// <summary>
        /// 添加招募信息失败
        /// </summary>
        /// <returns></returns>
        public ActionResult EditFaild()
        {
            ViewData["UserCenterSiteMap"] = new List<UserCenterSiteMapVModel>() { 
                new UserCenterSiteMapVModel()
                {
                    DisplayName="企业中心",
                    Link="/CompanyCenter/Home"
                },
                new UserCenterSiteMapVModel()
                {
                    DisplayName="发布招募失败",
                    Link="/CompanyCenter/PostFaild"
                }
            };
            return View();
        }
        /// <summary>
        /// 删除一个正在进行的招募
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int DeleteRecruit(int id)
        {
            if (new RecruitService(HttpContext.User.Identity.Name).RemoveRecruitment(id))
            {
                return 1;
            }
            return 0;
        }
        /// <summary>
        /// 查看招募详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult RecruitDetail(int id)
        {
            ViewData["UserCenterSiteMap"] = new List<UserCenterSiteMapVModel>() { 
                new UserCenterSiteMapVModel()
                {
                    DisplayName="企业中心",
                    Link="/CompanyCenter/Home"
                },
                new UserCenterSiteMapVModel()
                {
                    DisplayName="招募管理",
                    Link="/CompanyCenter/Recruit"
                },
                new UserCenterSiteMapVModel()
                {
                    DisplayName="招募详情",
                    Link="/CompanyCenter/RecruitDetail"
                }
            };
            ViewData["Recruiting"] = new RecruitService(HttpContext.User.Identity.Name).GetRecruitment(id);
            return View();
        }
        public ActionResult EditRecruit(int id)
        {
            ViewData["UserCenterSiteMap"] = new List<UserCenterSiteMapVModel>() { 
                new UserCenterSiteMapVModel()
                {
                    DisplayName="企业中心",
                    Link="/CompanyCenter/Home"
                },
                new UserCenterSiteMapVModel()
                {
                    DisplayName="招募管理",
                    Link="/CompanyCenter/Recruit"
                },
                new UserCenterSiteMapVModel()
                {
                    DisplayName="编辑招募",
                    Link="/CompanyCenter/EditRecruit"
                }
            };
            ViewData["Recruiting"] = new RecruitService(HttpContext.User.Identity.Name).GetRecruitment(id); 
            return View();
        }
        /// <summary>
        /// 职位详情
        /// </summary>
        /// <param name="id">PositionId</param>
        /// <returns></returns>
        public ActionResult PositionDetail(int id, int page = 0)
        {
            ViewData["UserCenterSiteMap"] = new List<UserCenterSiteMapVModel>() { 
                new UserCenterSiteMapVModel()
                {
                    DisplayName="企业中心",
                    Link="/CompanyCenter/Home"
                },
                new UserCenterSiteMapVModel()
                {
                    DisplayName="招募管理",
                    Link="/CompanyCenter/Recruit"
                },
                new UserCenterSiteMapVModel()
                {
                    DisplayName="职位详情",
                    Link="/CompanyCenter/PositionDetail"
                }
            };
            var position = new RecruitService(HttpContext.User.Identity.Name)
                .PositionDetail(id, page, 15);
            ViewData["Position"] = position;
            return View();
        }
        [HttpPost]
        public ActionResult CloseRecruiting(int id)
        {
            return View();
        }
        [HttpPost]
        public int ControlResume(int id, int type)
        {
            return new RecruitService(HttpContext.User.Identity.Name)
                .ControlResume(id, type);
        }
        #endregion

        #region 发布招募
        public ActionResult Recruiting()
        {
            ViewData["UserCenterSiteMap"] = new List<UserCenterSiteMapVModel>() { 
                new UserCenterSiteMapVModel()
                {
                    DisplayName="企业中心",
                    Link="/CompanyCenter/Home"
                },
                new UserCenterSiteMapVModel()
                {
                    DisplayName="兼职发布",
                    Link="/CompanyCenter/Recruiting"
                }
            };
            //代理企业信息
            var employerData = new RecruitService(HttpContext.User.Identity.Name).GetTempleteCompanies(0, 8);
            ViewData["EmployerStr"] = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(employerData);
            
            //历史招募单
            var recruitingTemplete = new RecruitService(HttpContext.User.Identity.Name).GetTempleteSheetes(0, 8);
            ViewData["RecruitingStr"] = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(recruitingTemplete);
            return View();
        }
        public string QueryRecruitingTemplete(int page = 0)
        {
            var recruitingTemplete = new RecruitService(HttpContext.User.Identity.Name).GetTempleteSheetes(page, 8);
            return new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(recruitingTemplete);
        }
        public string QueryCompanyTemplete(int page = 0)
        {
            //查询获得此代理人所维护的代理企业信息
            var employerData = new RecruitService(HttpContext.User.Identity.Name).GetTempleteCompanies(page, 8);
            //历史代理招募企业信息。json格式
            return new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(employerData);
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Recruiting(RecruitingVModel model)
        {
            DepositCompanyModel depositCompany = new DepositCompanyModel()
            {
                Agent = HttpContext.User.Identity.Name,
                Id = model.DepositCompanyId,
                CompanyBusiness = (Business)model.CompanyBusiness,
                CompanyDetail = model.CompanyDetail,
                CompanyDimensions = (CompanyDimensions)model.CompanyDimensions,
                CompanyName = model.CompanyName,
                CompanyType = (CompanyType)model.CompanyType,
                PostDate = DateTime.Now,
                Templete = model.CreateCompanyTemp == "on"
            };

            RecruitSheetModel recruitSheet = new RecruitSheetModel()
            {
                //Description = model.Description,
                Caption = model.Caption,
                PublishDate = DateTime.Now,
                Recruiter = HttpContext.User.Identity.Name,
                RecruitSubject = RecruitSheetSubject.Default,
                RecruitType = model.RecruitType == "on"? RecruitSheetType.Durable: RecruitSheetType.Normal,// RecruitSheetType.Normal,
                EnableEmail = model.EnableEmail == "on" ? true : false,
                WorkArea = (AreaCode)model.WorkArea,
                WorkAddress = model.WorkAddress,
                Contact = model.ContactMan,
                ContactPhone = model.ContactPhone,
                Templete = model.CreateTempelte == "on",
                DepositCompany = depositCompany,
                ReceiveEmail = model.ReceiveEmail,
                QQ = model.QQ,
                MapSetting = model.Map,
                Positions = CollectPosition(model.Position, Request.Form)
                    .Select(c => new PositionModel()
                    {
                        Degree = (Degree)c.Degree,
                        Experienced = c.Experienced == "on",
                        Gender = (Gender)c.Gender,
                        PositionDescript = c.PositionDescript,
                        PositionName = c.PositionName,
                        PositionType = (PositionType)c.PositionType,
                        IsIndex = false,
                        MaxSalary = c.MaxSalary,
                        MinSalary = c.MinSalary,
                        OperatorName = String.Empty,
                        Period = (SalaryPeriod)c.Period,
                        RecruitId = 0,
                        Trusteeship = false,
                        Vacancy = c.Vacancy,
                        ScanTime = 0,
                        Priority = (PositionPriority)(c.PositionPriority == "on" ? 1 : 0),
                        PositionLevel = PositionLevel.Normal
                    }).ToList()
            };
  
            if (new RecruitService(HttpContext.User.Identity.Name).NewRecruit(
                recruitSheet) != null)
            {
                return Redirect("/companycenter/postsuccess");
            }
            else
            {
                return Redirect("/companycenter/postfaild");
            }
        }
        private List<PositionVModel> CollectPosition(int Position, NameValueCollection form)
        {
            List<PositionVModel> list = new List<PositionVModel>();
            for (int i = 0; i < Position; i++)
            {
                if (form["PositionName" + i] != null)
                {
                    list.Add(new PositionVModel()
                    {
                        MaxSalary = form["MaxSalary" + i].ToInt32(0),
                        MinSalary = form["MinSalary" + i].ToInt32(0),
                        Period = form["Period" + i].ToInt32(0),
                        PositionDescript = form["PositionDescript" + i].Replace("\r\n", "<br/>"),
                        PositionName = form["PositionName" + i],
                        Vacancy = form["Vacancy" + i].ToInt32(0),
                        Degree = form["Degree" + i].ToInt32(0),
                        Gender = form["Gender" + i].ToInt32(0),
                        Experienced = form["Experienced" + i],
                        PositionType = form["PositionType" + i].ToInt32(0),
                        PositionPriority = form["Priority"]
                    });
                }
            }
            return list;
        }
        /// <summary>
        /// 恢复招募单信息，从模板
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string GetRecruitingTempelte(int id)
        {
            var item = new RecruitService(HttpContext.User.Identity.Name)
                .GetRecruitment(id);
            return item.ToJson();
        }
        /// <summary>
        /// 恢复企业信息，从模板
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string GetCompanyTemplete(int id)
        {
            var company = new RecruitService(HttpContext.User.Identity.Name)
                .GetDepositCompany(id);
            return company.ToJson();
        }
        /// <summary>
        /// 添加招募信息成功
        /// </summary>
        /// <returns></returns>
        public ActionResult PostSuccess()
        {
            ViewData["UserCenterSiteMap"] = new List<UserCenterSiteMapVModel>() { 
                new UserCenterSiteMapVModel()
                {
                    DisplayName="企业中心",
                    Link="/CompanyCenter/Home"
                },
                new UserCenterSiteMapVModel()
                {
                    DisplayName="兼职发布成功",
                    Link="/CompanyCenter/PostSuccess"
                }
            };
            return View();
        }
        /// <summary>
        /// 添加招募信息失败
        /// </summary>
        /// <returns></returns>
        public ActionResult PostFaild()
        {
            ViewData["UserCenterSiteMap"] = new List<UserCenterSiteMapVModel>() { 
                new UserCenterSiteMapVModel()
                {
                    DisplayName="企业中心",
                    Link="/CompanyCenter/Home"
                },
                new UserCenterSiteMapVModel()
                {
                    DisplayName="兼职发布失败",
                    Link="/CompanyCenter/PostFaild"
                }
            };
            return View();
        }
        #endregion

        #region 人才库
        public ActionResult Repository(int page=0)
        {
            ViewData["UserCenterSiteMap"] = new List<UserCenterSiteMapVModel>() { 
                new UserCenterSiteMapVModel()
                {
                    DisplayName="企业中心",
                    Link="/CompanyCenter/Home"
                },
                new UserCenterSiteMapVModel()
                {
                    DisplayName="人才库",
                    Link="/CompanyCenter/Repository"
                }
            };
            ViewData["Data"] = new RecruitService(HttpContext.User.Identity.Name)
                .QueryOfTalent(page, 20);
            return View();
        }
        #endregion

        #region 代理企业
        public ActionResult DepositCompany(int page = 0)
        {
            ViewData["UserCenterSiteMap"] = new List<UserCenterSiteMapVModel>() { 
                new UserCenterSiteMapVModel()
                {
                    DisplayName="企业中心",
                    Link="/CompanyCenter/Home"
                },
                new UserCenterSiteMapVModel()
                {
                    DisplayName="代理企业",
                    Link="/CompanyCenter/DepositCompany"
                }
            };
            ViewData["Data"] = new FirmService(HttpContext.User.Identity.Name)
                .QueryOfDepositCompany(page, 20);
            return View();
        }
        public int DeleteDepositCompany(int id)
        {
            return new FirmService(HttpContext.User.Identity.Name)
                .DeleteDepositCompany(id) ? 1 : 0;
        }
        public string GetDeposiCompany(int id)
        {
            var item = new FirmService(HttpContext.User.Identity.Name)
                .GetDepositCompany(id);
            return (new
            {
                CompanyName = item.CompanyName,
                CompanyType = item.CompanyType.Mapping(),
                CompanyDimensions = item.CompanyDimensions.Mapping(),
                CompanyBusiness = item.CompanyBusiness.Mapping(),
                CompanyDetail = item.CompanyDetail
            }).ToJson();
        }
        #endregion

        #region 企业信息
        public ActionResult AboutUs()
        {
            var item = new FirmService(HttpContext.User.Identity.Name).GetCorporation(true);
            ViewData["UserCenterSiteMap"] = new List<UserCenterSiteMapVModel>() { 
                new UserCenterSiteMapVModel()
                {
                    DisplayName="企业中心",
                    Link="/CompanyCenter/Home"
                },
                new UserCenterSiteMapVModel()
                {
                    DisplayName="企业信息",
                    Link="/CompanyCenter/AboutUs"
                }
            };
            AboutUsVModel model = new AboutUsVModel()
            {
                About = item.About,
                Agent = item.Agent,
                CompanyName = item.CompanyName,
                EMail = item.EMail,
                Id = item.Id,
                //Identification = item.Identification,
                Location = item.Location,
                Phone = item.Phone,
                QQ = item.AgentInfo.QQ,
                CertificationLevel=item.CertificationLevel
            };
            return View(model);
        }
        [HttpPost]
        public string UpdateCoporationLogo(string id)
        {
            string message = Upload(id);
            UpdateCoporationLogoModel model = message.ToObject<UpdateCoporationLogoModel>();
            if (model.result == 1)
            {
                string file = model.svepath.Replace("/Temp/", "/Images/");
                file = GetNewName(Server.MapPath(file));
                try
                {
                    System.IO.File.Copy(Server.MapPath(model.svepath),
                      file, false);
                    System.IO.File.Delete(Server.MapPath(model.svepath));
                    var user = new UserService(HttpContext.User.Identity.Name).GetUser();
                    user.Image = "/" + file.Replace(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "").Replace("\\", "/");
                    if (user.PostModify() != null)
                    {
                        model.svepath = user.Image;
                    }
                    return model.ToJson();
                }
                catch { }
            }
            return message;
        }
        private string GetNewName(string filePath, int circle = 1)
        {
            if (System.IO.File.Exists(filePath))
            {
                var fileInfo = new FileInfo(filePath);
                string name = fileInfo.Name;
                string newName = string.Empty;
                if (circle == 1) //如果是第一次递归
                {
                    newName = name.Substring(0, name.LastIndexOf('.')) + "(1)" + fileInfo.Extension;
                }
                else
                {
                    newName = name.Substring(0, name.LastIndexOf('(') + 1) + circle + ")" + fileInfo.Extension;
                }
                filePath = filePath.Replace(name, newName);
                filePath = GetNewName(filePath, ++circle);
            }
            return filePath;
        }       
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult UpCoporation(AboutUsVModel model)
        {
            model.About = model.About.NoHTMLSegment("script");
            var service = new FirmService(HttpContext.User.Identity.Name);
            var corp = service.GetCorporation();
            if (service.UpdateCorporation(new CorporationModel()
                {
                    About = model.About,
                    Agent = HttpContext.User.Identity.Name,
                    CompanyName = model.CompanyName,
                    EMail = model.EMail,
                    Id = model.Id,
                    //Identification = model.Identification,
                    Location = model.Location,
                    Phone = model.Phone,
                    CertificationLevel = corp.CertificationLevel
                }) != null)
            {
                //service.RemoveCacheOfCorporation();
                var usermodel = new UserService(HttpContext.User.Identity.Name).GetUser();
                if (usermodel != null && usermodel.QQ != model.QQ)
                {
                    usermodel.QQ = model.QQ.Trim();
                    usermodel.PostModify();
                }
                return Redirect("/companycenter/udcopsuccess");
            }
            return Redirect("/companycenter/udcopfail");
        }
        public ActionResult UdCopSuccess()
        {
            ViewData["UserCenterSiteMap"] = new List<UserCenterSiteMapVModel>() { 
                new UserCenterSiteMapVModel()
                {
                    DisplayName="企业中心",
                    Link="/CompanyCenter/Home"
                },
                new UserCenterSiteMapVModel()
                {
                    DisplayName="企业信息",
                    Link="/CompanyCenter/AboutUs"
                },
                new UserCenterSiteMapVModel()
                {
                    DisplayName="更新企业信息成功",
                    Link="/CompanyCenter/UdCopSuccess"
                }
            };
            return View();
        }
        public ActionResult UdCopFail()
        {
            ViewData["UserCenterSiteMap"] = new List<UserCenterSiteMapVModel>() { 
                new UserCenterSiteMapVModel()
                {
                    DisplayName="企业中心",
                    Link="/CompanyCenter/Home"
                },
                new UserCenterSiteMapVModel()
                {
                    DisplayName="企业信息",
                    Link="/CompanyCenter/AboutUs"
                },
                new UserCenterSiteMapVModel()
                {
                    DisplayName="更新企业信息失败",
                    Link="/CompanyCenter/UdCopFail"
                }
            };
            return View();
        }
        public ActionResult Authentication()
        {
            ViewData["UserCenterSiteMap"] = new List<UserCenterSiteMapVModel>() { 
                new UserCenterSiteMapVModel()
                {
                    DisplayName="企业中心",
                    Link="/CompanyCenter/Home"
                },
                new UserCenterSiteMapVModel()
                {
                    DisplayName="企业认证",
                    Link="/CompanyCenter/Authentication"
                }
            };
            var corp = new FirmService(HttpContext.User.Identity.Name).GetCorporation();
            ViewData["Corp"] = corp;
            //if (corp.CertificationId > 0)
            //{
            //    var info = new FirmService(HttpContext.User.Identity.Name).LoadCertificationInfo(corp.CertificationId).Support;
            //    if (info != null && info.Any())
            //    {
            //        ViewData["certPhoto"] = info[0].Content;
            //    }
            //}
            return View();
        }
        //[HttpPost]
        //public ActionResult Authentication(string PhotoUri)
        //{
        //    string file = PhotoUri.Replace("/Temp/", "/Images/");
        //    file = GetNewName(Server.MapPath(file));
        //    try
        //    {
        //        System.IO.File.Copy(Server.MapPath(PhotoUri),
        //            file, false);
        //        System.IO.File.Delete(Server.MapPath(PhotoUri));

        //        PhotoUri = "/" + file.Replace(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "").Replace("\\", "/");

        //    }
        //    catch (Exception e)
        //    {
        //        LogHelper.Write(CommonLogger.Zone, LogLevel.Error, e);
        //    }

        //    if (!String.IsNullOrEmpty(PhotoUri) && new FirmService(HttpContext.User.Identity.Name)
        //        .Examine(new CertificationModel()
        //        {
        //            Agent = HttpContext.User.Identity.Name,
        //            CertificationType = CertificationType.Company,
        //            Certified = false,
        //            DeliverWay = DeliverWay.Online,
        //            PostDate = DateTime.Now,
        //            Support = new List<SupportSheet>() { 
        //                new SupportSheet(){
        //                    Agent=HttpContext.User.Identity.Name,
        //                    Content=PhotoUri,
        //                    postDate=DateTime.Now,
        //                    SupportType = 0
        //                }
        //            }
        //        }) != null)
        //    {
        //        return RedirectToAction("PostAuthenticationSuccess");
        //    }
        //    return RedirectToAction("PostAuthenticationFail");
        //}
        //private string GetNewName(string filePath, int circle = 1)
        //{
        //    if (System.IO.File.Exists(filePath))
        //    {
        //        var fileInfo = new FileInfo(filePath);
        //        string name = fileInfo.Name;
        //        string newName = string.Empty;
        //        if (circle == 1) //如果是第一次递归
        //        {
        //            newName = name.Substring(0, name.LastIndexOf('.')) + "(1)" + fileInfo.Extension;
        //        }
        //        else
        //        {
        //            newName = name.Substring(0, name.LastIndexOf('(') + 1) + circle + ")" + fileInfo.Extension;
        //        }
        //        filePath = filePath.Replace(name, newName);
        //        filePath = GetNewName(filePath, ++circle);
        //    }
        //    return filePath;
        //}
        //public ActionResult PostAuthenticationFail()
        //{
        //    ViewData["UserCenterSiteMap"] = new List<UserCenterSiteMapVModel>() { 
        //        new UserCenterSiteMapVModel()
        //        {
        //            DisplayName="企业中心",
        //            Link="/CompanyCenter/Home"
        //        },
        //        new UserCenterSiteMapVModel()
        //        {
        //            DisplayName="企业认证",
        //            Link="/CompanyCenter/AboutUs"
        //        },
        //        new UserCenterSiteMapVModel()
        //        {
        //            DisplayName="提交认证材料失败",
        //            Link="/CompanyCenter/PostAuthenticationFail"
        //        }
        //    };
        //    ViewData["User"] = new UserService(HttpContext.User.Identity.Name).GetUser();
        //    return View();
        //}
        //public ActionResult PostAuthenticationSuccess()
        //{
        //    ViewData["UserCenterSiteMap"] = new List<UserCenterSiteMapVModel>() { 
        //        new UserCenterSiteMapVModel()
        //        {
        //            DisplayName="企业中心",
        //            Link="/CompanyCenter/Home"
        //        },
        //        new UserCenterSiteMapVModel()
        //        {
        //            DisplayName="企业认证",
        //            Link="/CompanyCenter/AboutUs"
        //        },
        //        new UserCenterSiteMapVModel()
        //        {
        //            DisplayName="提交认证材料成功",
        //            Link="/CompanyCenter/PostAuthenticationSuccess"
        //        }
        //    };
        //    ViewData["User"] = new UserService(HttpContext.User.Identity.Name).GetUser();
        //    return View();
        //}
        //public string Upload(string id)
        //{
        //    var files = System.Web.HttpContext.Current.Request.Files;
        //    if (files.Count > 0)
        //    {
        //        var uploadName = files.Keys[0];
        //        try
        //        {
        //            var item = new FileMonitor(String.Empty).Upload(
        //                new UploadFileSetting()
        //                {
        //                    BasePath = "/Customer/Temp",
        //                    TypeFilter = new List<FileType>() { 
        //                    FileType.jpg,
        //                    FileType.png
        //                }
        //                },
        //                new List<UpLoadFileItem>() { 
        //                new UpLoadFileItem(){
        //                    CopyMode= CopyMode.rename,
        //                    CustomerFileName=string.Empty,
        //                    File=System.Web.HttpContext.Current.Request.Files["Filedata"],
        //                    FileControlName="Filedata"
        //                }
        //            });
        //            return "{'result':'" + (item["Filedata"].IsSuccess ? 1 : 0) +
        //                "','err':'" + item["Filedata"].Error +
        //                "',svepath:'" + item["Filedata"].ServerPath + "'}";
        //        }
        //        catch (Exception e)
        //        {
        //            LogHelper.Write(CommonLogger.WebError, LogLevel.Error, e);
        //        }
        //    }
        //    return "{'result':'0','err':'不存在任何上次文件',svepath:''}"; ;
        //}
        #endregion

        #region 账户与安全
        public ActionResult Security()
        {
            ViewData["UserCenterSiteMap"] = new List<UserCenterSiteMapVModel>() { 
                new UserCenterSiteMapVModel()
                {
                    DisplayName="企业中心",
                    Link="/CompanyCenter/Home"
                },
                new UserCenterSiteMapVModel()
                {
                    DisplayName="修改密码",
                    Link="/CompanyCenter/Security"
                }
            };
            return View(new SecurityVModel());
        }
        [HttpPost]
        public int Security(SecurityVModel model)
        {
            var service = new UserService(HttpContext.User.Identity.Name);
            var user = service.GetUser();
            if (user == null || model.Confirm != model.NewPassword)
            {
                return 0;
            }
            else
            {
                return service.ChangePassword(model.OldPassword, model.NewPassword) != null ? 1 : 0;
            }
        }
        #endregion

        #region 客服支付
        public ActionResult ConstomerServcie()
        {
            ViewData["UserCenterSiteMap"] = new List<UserCenterSiteMapVModel>() { 
                new UserCenterSiteMapVModel()
                {
                    DisplayName="企业中心",
                    Link="/CompanyCenter/Home"
                },
                new UserCenterSiteMapVModel()
                {
                    DisplayName="客服与咨询",
                    Link="/CompanyCenter/constomerservcie"
                }
            };
            return View();
        }
        public ActionResult Suggestion()
        { 
             ViewData["UserCenterSiteMap"] = new List<UserCenterSiteMapVModel>() { 
                new UserCenterSiteMapVModel()
                {
                    DisplayName="企业中心",
                    Link="/CompanyCenter/Home"
                },
                new UserCenterSiteMapVModel()
                {
                    DisplayName="客服与咨询",
                    Link="/CompanyCenter/suggestion"
                }
            };
            return View();
        }
        #endregion

        public string Upload(string id)
        {
            var files = System.Web.HttpContext.Current.Request.Files;
            if (files.Count > 0)
            {
                var uploadName = files.Keys[0];
                var item = new FileMonitor(String.Empty).Upload(
                    new UploadFileSetting()
                    {
                        BasePath = "/Customer/Temp",
                        TypeFilter = new List<FileType>() { 
                            FileType.jpg,
                            FileType.png
                        }
                    },
                    new List<UpLoadFileItem>() { 
                        new UpLoadFileItem(){
                            CopyMode= CopyMode.rename,
                            CustomerFileName=string.Empty,
                            File=System.Web.HttpContext.Current.Request.Files["Filedata"],
                            FileControlName="Filedata"
                        }
                    });
                return "{'result':'" + (item["Filedata"].IsSuccess ? 1 : 0) +
                    "','err':'" + item["Filedata"].Error +
                    "',svepath:'" + item["Filedata"].ServerPath + "'}";
            }
            return "{'result':'0','err':'不存在任何上次文件',svepath:''}"; ;
        }
    }
}
